<template>
    <div class="ambientLightWarp" v-if="dataObj !== null">
        <div class="title" @click="slideToggleAni">
            <button class="el-icon-caret-right tagIco"></button>
            <span>{{dataObj.name}}</span>
            <button class="el-icon-delete deleteIco" @click="deleteClicked"></button>
            <el-switch v-model="switchFlag.visible" inactive-color="rgba(52, 54, 74, 1)"
                       active-color="rgba(31, 33, 49, 1)" class="switchBtn" :change="switchOnChanged">
            </el-switch>
        </div>

        <div class="lightContainer">
            <div class="categoryItem">
                <span class="categoryItemTitle">坐标</span>

                <span class="position">
                <div>
                    <span class="shadowBox">&nbsp; X
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.position.x.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['position','x'])">
                    </span>
                     <span class="shadowBox">&nbsp; Y
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.position.y.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['position','y'])">
                    </span>
                    <span class="shadowBox">&nbsp; Z
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.position.z.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['position','z'])">
                    </span>
                 </div>
             </span>
            </div>

            <div class="categoryItem">
                <span class="categoryItemTitle">旋转</span>

                <span class="position">
                <div>
                    <span class="shadowBox">&nbsp; X
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.rotation._x.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['rotation','x'])">
                    </span>
                     <span class="shadowBox">&nbsp; Y
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.rotation.y.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['rotation','y'])">
                    </span>
                    <span class="shadowBox">&nbsp; Z
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.rotation.z.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['rotation','z'])">
                    </span>
                 </div>
             </span>
            </div>

            <div class="categoryItem">
                <span class="categoryItemTitle">缩放</span>

                <span class="position">
                <div>
                    <span class="shadowBox">&nbsp; X
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.scale.x.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['scale','x'])">
                    </span>
                     <span class="shadowBox">&nbsp; Y
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.scale.y.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['scale','y'])">
                    </span>
                    <span class="shadowBox">&nbsp; Z
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.scale.z.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['scale','z'])">
                    </span>
                 </div>
             </span>
            </div>

            <div class="categoryItem">
                <ColorPicker :color="dataObj.color" :colorType="colorType"/>
            </div>

            <div class="categoryItem">
                <WeightPicker :data="dataObj"/>
            </div>
        </div>


    </div>
</template>

<script>

    import $ from "jquery";
    import DomUtil from "@/utils/baseUtil.js";

    import ColorPicker from "@/components/styleComponents/colorPickerCmp.vue";
    import WeightPicker from "@/components/styleComponents/weightPickerCmp.vue";

    import Vue from "vue";

    export default {
        name: "ambientLight",

        components: {
            ColorPicker,
            WeightPicker
        },

        data () {
            return {
                switchFlag: this.$props.data,
                dataObj: this.$props.data,
                colorType: "颜色",
            }
        },

        watch : {
            data (val) {
                this.dataObj = val || null;
            }
        },

        props: ["data"],

        mounted() {

            if (this.dataObj.castShadow){
                $(this.$refs.radioBtn).addClass("el-icon-check");
            }

            const _this = this;

            this.$root.$on("PointLightValueChanged", _this.reloadData);
        },

        methods: {
            toggleClass (e, val) {

                $(e.target).toggleClass("el-icon-check");

                val = !val;

                this.dataObj.castShadow = val;
            },

            slideToggleAni (e) {
                let target = e.target;

                if (!$(target).hasClass("title") && !$(target).hasClass("el-switch__core") &&
                    !$(target).hasClass("el-icon-delete deleteIco")) {
                   target = $(target).parent(".title");
                }

                if (!$(target).hasClass("el-icon-delete deleteIco")) {
                    $(target).toggleClass('titleActive');
                    $(target).siblings().eq(0).slideToggle(200);
                }
            },

            changeVal (e, data, paramList) {

                if (e.keyCode === 13 && paramList.length > 0) {

                    const target = e.target;

                    let value = {};

                    paramList.forEach(attr => value = value[attr] || data[attr] || 0);

                    this.$store.dispatch('edited', {
                        target: data,
                        value: value,
                        paramList: paramList
                    });

                    DomUtil.changeVal(target, data, paramList);

                }

            },

            switchOnChanged () {
                this.switchFlag = !this.switchFlag;
            },

            deleteClicked () {

                if (this.dataObj.parent !== null) {

                    this.dataObj.parent.remove(this.dataObj);

                    /* eslint-disable */
                    Vue.prototype.baseConfig.light = Editor.getLights();

                }

                this.$root.$emit("outLineDataChanged");
                this.$root.$emit("editorDataChanged");
            },

            reloadData () {

                this.$forceUpdate();
            }
        }
    }
</script>

<style scoped>
    .ambientLightWarp {
        width: 320px;
        padding-bottom: 14px;
        color: rgba(182, 184, 220, 1);
        box-sizing: border-box;
        margin: 4px auto;
        border-radius: 5px;
        position: relative;
    }

    .title {
        width: 100%;
        height: 30px;
        background:rgba(65,67,91,1);
        font-size: 12px;
        border-radius: 5px;
        position: relative;
        line-height: 30px;
        cursor: pointer;
    }

    .titleActive {
        background-color: rgba(105, 108, 145, 1);
        border-radius: 5px 5px 0 0;
    }

    .titleActive .el-icon-caret-right.tagIco {
        transition: .5s all;
        transform: rotateZ(90deg);
    }

    .lightContainer {
        background-color: rgba(52,54,74,1);
        border-radius: 0 0 5px 5px;
        display: none;
    }

    .el-icon-caret-right.tagIco {
        position: absolute;
        left: 14px;
        line-height: 30px;
        transition: .5s all;
        transform: rotateZ(0);
    }

    .title span {
        position: absolute;
        left: 34px;
        max-width: 120px;
        overflow: hidden;
    }

    .title:hover span, .title:hover .el-icon-caret-right.tagIco {
        color: rgba(213, 215, 250, 1);
    }

    .el-icon-delete.deleteIco {
        position: absolute;
        right: 70px;
        top: 9px;
    }

    .switchBtn {
        position: absolute;
        right: 15px;
        top: 5px;
    }

    .position {
        display: inline-block;
        width: 220px;
        height: 30px;
        background-color: rgba(31, 33, 49, 1);
        border-radius:5px;
        line-height: 30px;
        margin-left: 44px;
    }

    .position div {
        display: flex;
        justify-content: space-around;
    }

    .positionInput, .shadowBoxSingleInput {
        border: none;
        background-color: transparent;
        width: 39px;
        color: rgba(182,184,220,1);
        font-size: 13px;
        vertical-align: baseline;
        box-sizing: border-box;
    }

    .shadowBox {
        font-size: 13px;
        font-weight: 300;
        color: rgba(65,67,91,1);
    }

    .shadowBoxSingle {
        font-size: 13px;
        font-weight: 300;
        color: rgba(65,67,91,1);
        display: inline-block;
        width: 40px;
        height: 30px;
        background-color: rgba(31, 33, 49, 1);
        border-radius:5px;
        line-height: 30px;
        padding-left: 5px;
        position: absolute;
        left: 100px;
    }

    .categoryItem {
        padding-top: 14px;
    }

    .categoryItemBottom {
        padding: 14px 0;
    }

    .categoryItemTitle {
        font-size: 12px;
        font-weight: 100;
        color: rgba(182,184,220,1);
        padding-left: 32px;
    }
</style>